
import DrawActionBase from "./DrawActionBase";
import DrawActionGraffiti from "./DrawActionGraffiti";
import DrawContext from "./DrawContext";
import GraphicType from "./GraphicType";
import DrawState from "./DrawState";

class Draw{
    static __instance = null;
    static instance(){
        if(!Draw.__instance){
            Draw.__instance = new Draw();
            DrawState.init();
        }
        return Draw.__instance;
    }

    leftClick(e){
        if(!this.drawAction) return;
        this.drawAction.leftClick(e);
    }

    leftDown(e){
        if(!this.drawAction) return;
        this.drawAction.leftDown(e);
    }

    leftUp(e){
        if(!this.drawAction) return;
        this.drawAction.leftUp(e);
    }

    mouseMove(e){
        if(!this.drawAction) return;
        this.drawAction.mouseMove(e);
    }



    constructor() {
        let viewer = window.cesiumViewer;
        let self = this;
        this.drawAction = new DrawActionBase();
        DrawContext.dataSource = new Cesium.CustomDataSource("DRAW");
        viewer.dataSources.add(DrawContext.dataSource);
        this.handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);

        this.handler.setInputAction(function (e) {self.mouseMove(e)}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        this.handler.setInputAction(function (e) {self.leftClick(e)}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        this.handler.setInputAction(function (e) {self.leftUp(e)}, Cesium.ScreenSpaceEventType.LEFT_UP);
        this.handler.setInputAction(function (e) {self.leftDown(e)}, Cesium.ScreenSpaceEventType.LEFT_DOWN);

        this._noneDraw = new DrawActionBase();
    }

    stopDraw(){
        if(this.drawAction){
            this.drawAction.stop();
        }
        this.drawAction = this._noneDraw;
    }

    undo(){
        if(this.drawAction){
            return this.drawAction.undo();
        }
        // return null;
    }

    drawGraffiti(fun){
        if(this.drawAction){
            this.drawAction.stop();
        }

        if(!this.drawAction || this.drawAction.type() != GraphicType.GRAFFITI){
            this.drawAction = new DrawActionGraffiti();
        }
        this.drawAction.setDrawCallback(fun);
    }

}

// Draw.init();
export default Draw;